# -*- coding: utf-8 -*-

# Bliotecas padrões
from django.forms import *
from django.forms.extras.widgets import SelectDateWidget
from django.forms.widgets import PasswordInput 
from django.contrib.admin.widgets import AdminFileWidget,AdminTimeWidget
from django.contrib.admin import widgets

# Bibliotecas externas

# Bibliotecas das apps
from choices import *
from cadastro.extras import AdminImageWidget

# Models
from myproject.aula.models import *

class CursoModelForm(ModelForm):
    class Meta:
        model = Curso

class AulaModelForm(ModelForm):
    
    class Meta:
        model = Aula
        fields = ('curso','nome')

    def __init__(self, *args, **kwargs):
        try:
            cod_curso = kwargs['cod_curso']
            kwargs = {}
        except:
            cod_curso = None
        super(AulaModelForm, self).__init__(*args, **kwargs)
        instance = getattr(self, 'instance', None)
        if instance.id:
          self.fields['curso'] = ModelChoiceField(label='Curso', queryset=Curso.objects.filter(id=instance.curso.id))
        elif cod_curso:
            qs = Curso.objects.filter(id=cod_curso)
            self.fields['curso'] = ModelChoiceField(label='Curso', queryset=qs,initial=qs[0])
          
class ArquivoAulaModelForm(ModelForm):
    
    class Meta:
        model = ArquivoAula
        fields = ('aula','arquivo')

    def __init__(self, *args, **kwargs):
        try:
            cod_aula = kwargs['cod_aula']
            kwargs = {}
        except:
            cod_aula = None
        super(ArquivoAulaModelForm, self).__init__(*args, **kwargs)
        instance = getattr(self, 'instance', None)
        self.fields['arquivo'].widget = AdminFileWidget()
        if instance.id:
            print 'TESTE '+str(instance.aula.id)
            print Aula.objects.filter(id=instance.aula.id).count()
            qs = Aula.objects.filter(id=instance.aula.id)
            self.fields['aula'] = ModelChoiceField(label='Aula', queryset=qs, initial=qs[0])
        elif cod_aula:
            qs = Aula.objects.filter(id=cod_aula)
            self.fields['aula'] = ModelChoiceField(label='Aula', queryset=qs,initial=qs[0])
        print self.fields
          
class ImagemAulaModelForm(ModelForm):
    
    class Meta:
        model = ImagemAula
        fields = ('aula','imagem')

    def __init__(self, *args, **kwargs):
        try:
            cod_aula = kwargs['cod_aula']
            kwargs = {}
        except:
            cod_aula = None
        super(ImagemAulaModelForm, self).__init__(*args, **kwargs)
        instance = getattr(self, 'instance', None)
        self.fields['imagem'].widget = AdminImageWidget()
        if instance.id:
            qs = Aula.objects.filter(id=instance.aula.id)
            self.fields['aula'] = ModelChoiceField(label='Aula', queryset=qs, initial=qs[0])
        elif cod_aula:
            qs = Aula.objects.filter(id=cod_aula)
            self.fields['aula'] = ModelChoiceField(label='Aula', queryset=qs, initial=qs[0])

class VideoAulaModelForm(ModelForm):
    
    class Meta:
        model = VideoAula
        fields = ('aula','video')

    def __init__(self, *args, **kwargs):
        try:
            cod_aula = kwargs['cod_aula']
            kwargs = {}
        except:
            cod_aula = None
        super(VideoAulaModelForm, self).__init__(*args, **kwargs)
        instance = getattr(self, 'instance', None)
        self.fields['video'].widget = AdminFileWidget()
        if instance.id:
          self.fields['aula'] = ModelChoiceField(label='Aula', queryset=Aula.objects.filter(id=instance.aula.id))
        elif cod_aula:
            qs = Aula.objects.filter(id=cod_aula)
            self.fields['aula'] = ModelChoiceField(label='Aula', queryset=qs,initial=qs[0])